package be.kuleuven.thesis.status;
//charles proxy
import java.util.ArrayList;
import java.util.List;

import be.kuleuven.thesis.framework.Song;

public class Player {
	public static final int SCROLL_DOWN = 1;
	
	private static Player instance;
	private List<Song> currentPlaylist = new ArrayList<Song>();
	private int currentIndex = 0;
	private boolean paused;
	private StatusListener listener;
	
	private Player() {
		
	}
	
	public static synchronized Player getInstance() {
		if(instance == null)
			return instance = new Player();
		return instance;
	}
	
	public int getCurrentIndex() {
		return currentIndex;
	}
	
	public int getPlaylistSize() {
		return currentPlaylist.size();
	}
	
	public Song getCurrentSong() {
		if(currentIndex == 0)
			return null;
		return currentPlaylist.get(currentIndex-1);
	}
	
	public List<Song> getCurrentPlaylist() {
		return currentPlaylist;
	}
	
	public boolean isPaused() {
		return paused;
	}
	public void pause() {
		getInstance().paused = true;
	}
	public void resumePlay() {
		if(getCurrentSong() == null && !getCurrentPlaylist().isEmpty()) {
			playSongInPlaylist(1);
			if(listener != null)
				listener.onCurrentSongChanged();
			//notifyObservers();
//			if(nowPlaying != null)
//				nowPlaying.notifyCurrentSongChanged();
		}
		paused = false;
	}
	
	private void resetPlaylist() {
		currentPlaylist.clear();
		PlaylistGenerator.getInstance().reset();
	}
	
	public void playSong(Song song) {
		resetPlaylist();
		currentPlaylist.add(song);
		PlaylistGenerator.getInstance().likeSong(song);
		currentIndex = 1;
		if(isPaused()) {
			resumePlay();
		}
	}
	
	public void addSongToCurrentPlaylist(Song song) {
		if(currentPlaylist.isEmpty()) {
			playSong(song);
		}
		else {
			currentPlaylist.add(song);
			PlaylistGenerator.getInstance().likeSong(song);
		}
	}
	
	public void playSongInPlaylist(int index) {
		currentIndex = index;
		if(isPaused()) {
			resumePlay();
		}
		if(listener != null)
			listener.onCurrentSongChanged();
		PlaylistGenerator.getInstance().checkGeneration();
	}
	
	public void addGeneratedSong(Song song) {
		currentPlaylist.add(song);
		if(currentIndex == 0) {
			playSongInPlaylist(currentPlaylist.size());
//			if(nowPlaying != null)
//				nowPlaying.notifyCurrentSongChanged();
		}
		if(listener != null)
			listener.onSongGenerated();
		//notifyObservers(SCROLL_DOWN);
	}
	
	public boolean canGoBack() {
		return !(getCurrentSong() == null || currentIndex == 1);
	}
	
	public boolean canSkip() {
		return getCurrentSong() != null;
	}
	
	public void skip() {
		if(!canSkip())
			return;
		if(currentIndex == currentPlaylist.size())
			endOfPlaylist();
		else currentIndex++;
		if(listener != null)
			listener.onCurrentSongChanged();
		//notifyObservers();
//		if(nowPlaying != null)
//			nowPlaying.notifyCurrentSongChanged();
		PlaylistGenerator.getInstance().checkGeneration();
	}
	
	public void goToPrevious() {
		if(!canGoBack())
			return;
		currentIndex--;
		if(listener != null)
			listener.onCurrentSongChanged();
		//notifyObservers();
		//nowPlaying.notifyCurrentSongChanged();
	}
	
	public void endOfPlaylist() {
		pause();
		currentIndex = 0;
	}
	
	public void removeSong(int index) {
		currentPlaylist.remove(index-1);
		if(currentIndex > index)
			currentIndex--;
		if(currentPlaylist.isEmpty()) {
			endOfPlaylist();
			//currentPlaylistView.setEmptyVisibility();
		}
		listener.onSongRemoved();
		//notifyObservers();
//		if(currentPlaylistView != null)
//			currentPlaylistView.getSongAdapter().notifyDataSetChanged();
		PlaylistGenerator.getInstance().checkGeneration();
	}
	
	public void clearPlaylist() {
		if(!isPaused())
			pause();
		currentPlaylist.clear();
		currentIndex = 0;
		PlaylistGenerator.getInstance().reset();
		listener.onPlaylistCleared();
	}
	
	public void setStatusListener(StatusListener listener) {
		this.listener = listener;
	}
}
